package tq.com.tqstudent.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.lib.common.base.BaseFragment;
import com.lib.common.utils.LogUtil;

import org.greenrobot.eventbus.Subscribe;

import butterknife.BindView;
import butterknife.OnClick;
import tq.com.tqstudent.App;
import tq.com.tqstudent.R;
import tq.com.tqstudent.bean.SimpleBaseBean;
import tq.com.tqstudent.bean.UserBean;
import tq.com.tqstudent.bean.UserInfo;
import tq.com.tqstudent.bean.event.LoginOutEvent;
import tq.com.tqstudent.ui.activity.LoginActivity;
import tq.com.tqstudent.ui.activity.RegisterActivity;
import tq.com.tqstudent.ui.activity.classes.ClassActivateActivity;
import tq.com.tqstudent.ui.activity.classes.CommentActivity;
import tq.com.tqstudent.ui.activity.classes.NoticeActivity;
import tq.com.tqstudent.ui.activity.classes.SwitchActivity;
import tq.com.tqstudent.ui.activity.mine.ActivateListActivity;
import tq.com.tqstudent.ui.activity.mine.BindAccountActivity;
import tq.com.tqstudent.ui.activity.mine.FeedbackActivity;
import tq.com.tqstudent.ui.activity.mine.FreeWatchActivity;
import tq.com.tqstudent.ui.activity.mine.ProfileActivity;
import tq.com.tqstudent.ui.activity.mine.SettingActivity;
import tq.com.tqstudent.ui.activity.mine.ShareActivity;
import tq.com.tqstudent.utils.VarietyUtil;
import tq.com.tqstudent.views.LoginTipsDialog;

/**
 * @Author rames
 * @date 2019/2/27 11:17
 */
public class MineFragment extends BaseFragment {
    public boolean isLoaded = false;
    @BindView(R.id.info_layout)
    LinearLayout infoLayout;
    @BindView(R.id.login_tv)
    TextView loginTV;
    @BindView(R.id.name_tv)
    TextView nameTV;
    @BindView(R.id.class_tv)
    TextView classTV;
    @BindView(R.id.head_iv)
    ImageView headIV;

    @Override
    protected int setView() {
        return R.layout.fragment_mine;
    }

    @Override
    protected void init(View view) {
        if (isVisible() && isLoaded) {
            return;
        }
        isLoaded = true;
    }

    @Override
    public void onResume() {
        super.onResume();
        initData();
    }

    private void initData() {
        if (UserInfo.isLogin()) {
            UserBean user = UserInfo.get();
            if (TextUtils.isEmpty(UserInfo.getHeadImg())) {
                headIV.setImageResource(R.mipmap.icon_head_default);
            } else {
                Glide.with(getActivity()).load(VarietyUtil.getHeadImgUri(UserInfo.getHeadImg())).into(headIV);
            }
            nameTV.setText(user.name);
            classTV.setText(VarietyUtil.getClassName(user.grade));
            loginTV.setVisibility(View.GONE);
            infoLayout.setVisibility(View.VISIBLE);
        } else {
            headIV.setImageResource(R.mipmap.icon_head_default);
            loginTV.setVisibility(View.VISIBLE);
            infoLayout.setVisibility(View.GONE);
        }
    }

    // 退出登录
    @Subscribe
    public void onEvent(LoginOutEvent event) {
        loginTV.setVisibility(View.VISIBLE);
        infoLayout.setVisibility(View.GONE);
        headIV.setImageResource(R.mipmap.icon_head_default);
    }

    @OnClick({R.id.info_layout, R.id.login_tv, R.id.activate_layout, R.id.free_watch_layout, R.id.bind_layout,
            R.id.pwd_layout, R.id.share_layout, R.id.setting_layout, R.id.feedback_layout, R.id.join_rl, R.id.switch_rl,
            R.id.notice_rl, R.id.comment_rl})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.login_tv:
                LoginActivity.show(getActivity());
                return;

            case R.id.share_layout:
                ShareActivity.show(getActivity());
                return;

            case R.id.feedback_layout:
                FeedbackActivity.show(getActivity());
                return;
        }

        if (!UserInfo.isLogin()) {
            new LoginTipsDialog(getActivity()).show();
            return;
        }

        switch (v.getId()) {
            case R.id.info_layout:
                ProfileActivity.show(getActivity());
                break;

            case R.id.setting_layout:
                SettingActivity.show(getActivity());
                break;

            case R.id.activate_layout:
                ActivateListActivity.show(getActivity());
                break;

            case R.id.free_watch_layout:
                FreeWatchActivity.show(getActivity());
                break;

            case R.id.bind_layout:
                BindAccountActivity.show(getActivity());
                break;

            case R.id.pwd_layout:
                RegisterActivity.show(getActivity(), RegisterActivity.PWD_TYPE.MODIFY_PWD);
                break;

            case R.id.join_rl:
                ClassActivateActivity.show(mActivity);
                break;

            case R.id.switch_rl:
                SwitchActivity.show(mActivity);
                break;

            case R.id.notice_rl:
                NoticeActivity.show(mActivity);
                break;

            case R.id.comment_rl:
                CommentActivity.show(mActivity);
                break;
        }
    }

    @Override
    protected void initData(Bundle savedInstanceState) {

    }
}
